﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using H2.DataTypes;
using H2.Streams;
using H2.Streams.Sections;

namespace Mystery.Unicode
{
    public class UnicodeIndex
    {
        public uint StringIDData;
        public StringID ReferenceID;
        public int StringOffset;

        public static UnicodeIndex[] Read(MapStream map, UnicodeInfo info)
        {
            int indexOffset = info.StringIndexOffset;
            int count = info.StringCount;
            map.Position = indexOffset;

            UnicodeIndex[] indices = new UnicodeIndex[count];
            for (int i = 0; i < count; i++)
            {
                UnicodeIndex ui = new UnicodeIndex();
                indices[i] = ui;

                ui.ReferenceID = map.ReadStringID(true);
                ui.StringIDData = map.ReadUInt32();
                ui.StringOffset = map.ReadInt32();
            }

            return indices;
        }

        public static void Write(MapStream map, UnicodeIndex[] indices)
        {
            for (int i = 0; i < indices.Length; i++)
            {
                UnicodeIndex ui = indices[i];
                if (map.StringIDs.Contains(ui.ReferenceID))
                    map.Write(ui.ReferenceID);
                else
                    map.Write(ui.StringIDData);
                    //map.Write(0);
                map.Write(ui.StringOffset);
            }
        }
    }
}
